package cn.group.controller;

import cn.group.service.GoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.entity.Good;
import utils.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "商品信息")
@Controller
@RequestMapping(value = "/good" , produces = "text/html;charset=UTF-8")
public class GoodController extends BaseController {

    @Autowired
    private GoodService goodService;

    @ApiOperation("搜索商品")
    @RequestMapping("/search.do")
    @ResponseBody
    public String getURLMassage(String title, String page, HttpServletResponse response){
        List<Good> urlMassage = goodService.getURLMassage(title, page);
        List<Good> goods=new ArrayList<>();
        urlMassage.forEach(item->{
            Good good = goodService.findGood(item);
            if(good==null){
                goods.add(item);
            }
        });
        String json = writerSuccessResult(goods, response);
        return json;
    }

    @ApiOperation("保存商品")
    @RequestMapping(value = "/addGoods.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String addGoods(@RequestBody Good paramsJson, HttpServletResponse response){
        goodService.addGood(paramsJson);
        String json = writerSuccessResult(paramsJson, response);
        return json;
    }

    @ApiOperation("展示商品")
    @RequestMapping("/findAllGoods.do")
    @ResponseBody
    public String findAllGoods(HttpServletResponse response){
        List<Good> allGoods = goodService.findAllGoods();
        String json = writerSuccessResult(allGoods, response);
        return json;
    }

    @ApiOperation("删除商品")
    @PostMapping(value = "/deleteGood.do")
    @ResponseBody
    public String deleteGood(Long goodId, HttpServletResponse response) {
        goodService.deleteGood(goodId);
        return writerSuccessResult("删除成功！", response);
    }

    @ApiOperation("修改状态为1")
    @PostMapping(value = "/up.do")
    @ResponseBody
    public String updateUp(Long goodId, HttpServletResponse response) {
        goodService.updateUp(goodId);
        return writerSuccessResult("修改成功！", response);
    }

    @ApiOperation("修改状态为0")
    @PostMapping(value = "/down.do")
    @ResponseBody
    public String updateDown(Long goodId, HttpServletResponse response) {
        goodService.updateDown(goodId);
        return writerSuccessResult("修改成功！", response);
    }

    @ApiOperation("模糊查询商品")
    @PostMapping(value = "/goods.do")
    @ResponseBody
    public String selectByName(String key, HttpServletResponse response) {
        System.out.println("这是key："+key);
        List<Good> goods = goodService.selectByName(key);
        goods.forEach(System.out::println);
        return writerSuccessResult(goods, response);
    }
}
